<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Course extends Model
{
    use SoftDeletes;

    public $table = 'courses';

    public $fillable = [
        'name',
        'category_id',
        'grade_id',
        'synopsis',
        'image_path',
        'recommend',
        'speaker_id'
    ];
    public function schoolManager(){
        return $this->belongsToMany(School::class,'school_has_tests_courses','lesson_test_id','school_id')
            ->where('school_has_tests_courses.product_type','lesson');
    }

    public function teachers(){
        return $this->belongsToMany(Teacher::class, 'teacher_has_courses', 'course_id','teacher_id')->withTrashed();

    }

    public function lessons(){
        return $this->hasMany(Lesson::class,'course_id','id');
    }

    /**
     * @param $categoryId
     * @return mixed
     */
    public static function getCategoryName($categoryId)
    {
        $model = CourseCategory::query()->where('id', $categoryId)->first();
        return $model->name;
    }

    /**
     * @param $gradeId
     * @return mixed
     */
    public static function getGradeName($gradeId)
    {
        $model = Grade::query()->where('id', $gradeId)->first();
        return $model->name;
    }


    /**
     * @param $id
     * @return mixed
     */
    public static function getName($id)
    {
        $model = self::query()->where('id', $id)->first();
        return $model->name;
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function speaker()
    {
        return $this->belongsTo(User::class);
    }


    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
//    public function products()
//    {
//        return $this->belongsToMany(Product::class);
//    }
}
